//
//  ExamConfirmViewController.m
//  jinnengExam
//
//  Created by mac on 2020/11/15.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "ExamConfirmViewController.h"
#import <MJExtension.h>
#import "OnlineExamingViewController.h"
@interface ExamConfirmViewController ()

@end

@implementation ExamConfirmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [super viewDidLoad];
    self.title = @"考试信息";
    UINavigationBar * bar = self.navigationController.navigationBar;
    UIImage *bgImage = [ColorUtil navigationBarColor:[ColorUtil titleColor] BarSize:bar.frame alphe:1.0];
    [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    [bar setTintColor:[UIColor whiteColor]];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]}forState:UIControlStateNormal];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    [self initView];
    
}

-(void) initView{
   [self.view addSubview:self.jznWebView];
    [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.right.equalTo(self.view);
         if (@available(iOS 11.0, *)) {
             make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
             make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
         } else {
             make.top.bottom.equalTo(self.view);
             make.bottom.equalTo(self.view);
         }
     }];
    self.jznWebView.scrollView.bounces = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/examConfirm" type:@"html"];
    [self registerJSFunction];
}

-(void) registerJSFunction {
    [self.webViewBridge registerHandler:@"toOnlineExam" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSString *url = [NSString stringWithFormat:@"%@/onlineExam/findOnlineExamPaper" ,[URLUtil hostName]];
        NSDictionary *params = @{@"userId":[NSString stringWithFormat:@"%d",self.loginDto.userId],
                                 @"examId":[NSString stringWithFormat:@"%d",[data[@"examId"]intValue]]};
        [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
               if(dataResult.code == 0){
                   dispatch_async(dispatch_get_main_queue(), ^(){
                      OnlineExamingViewController *vc = [[OnlineExamingViewController alloc]init];
                      [vc setPaper:[dataResult.data mj_JSONString]];
                      [vc setOnlineExamUserId:[data[@"onlineExamUserId"]intValue]];
                       [vc setTime:[data[@"examTime"]intValue]*60];
                      [self.navigationController pushViewController:vc animated:YES];
                  });
               }else {
                   
                   [[UIApplication sharedApplication].delegate.window makeToast:dataResult.msg];
               }
           } failure:^(NSError *error) {
               
           }];
    }];

}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    NSString *js = [NSString stringWithFormat:@"getExam(%@)",self.examJson];
    [self.jznWebView evaluateJavaScript:js completionHandler:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
